﻿namespace WMS6.Web.Administration
{
    using System;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoDetailServiceRef;

    public class SysCodeDetail : BasePage
    {
        protected Button btnCancel;
        protected Button btnFinish;
        protected Button btnSaveContinue;
        protected CheckBox chkIsActive;
        protected CheckBox chkSystemcreated;
        protected HtmlForm F1083;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnCreatedBy;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnSysCodeType;
        protected HtmlInputHidden hdnType;
        protected Label Label1;
        protected Label lblDescription;
        protected Label lblMsg;
        protected Label lblRemark;
        protected Label lblSysCode;
        protected Label lblSysCodeDetail;
        protected TextBox txtDescription;
        protected TextBox txtRemark;
        protected TextBox txtSysCode;

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            base.Server.Transfer(string.Format("SysCode.aspx?from={0}&detail={1}&ID={2}", base.UrlEncode(this.hdnFrom.Value), "2", base.UrlEncode(this.hdnSysCodeType.Value)), false);
        }

        protected void btnFinish_Click(object sender, EventArgs e)
        {
            bool success = false;
            if (!string.IsNullOrEmpty(this.txtSysCode.Text))
            {
                try
                {
                    SysCodeDetailInfo sysCodeDetail = new SysCodeDetailInfo {
                        SysCodeType = this.hdnSysCodeType.Value,
                        Remark = this.txtRemark.Text,
                        Description = this.txtDescription.Text
                    };
                    if (this.chkIsActive.Checked)
                    {
                        sysCodeDetail.IsActive = "Y";
                    }
                    else
                    {
                        sysCodeDetail.IsActive = "N";
                    }
                    if (this.chkSystemcreated.Checked)
                    {
                        sysCodeDetail.SystemCreated = "Y";
                    }
                    else
                    {
                        sysCodeDetail.SystemCreated = "N";
                    }
                    sysCodeDetail.SysCode = this.txtSysCode.Text;
                    if (this.hdnType.Value == "Add")
                    {
                        sysCodeDetail.CreatedBy = base.UserInfo.UserID;
                        if (!string.IsNullOrEmpty(BaseInfoDetailServiceClientProxy.GetInstance().InsertDetail(sysCodeDetail, "SYS_CODE_DETAIL")))
                        {
                            success = true;
                        }
                    }
                    else
                    {
                        sysCodeDetail.CreatedBy = this.hdnCreatedBy.Value;
                        sysCodeDetail.UpdatedBy = base.UserInfo.UserID;
                        if (!string.IsNullOrEmpty(BaseInfoDetailServiceClientProxy.GetInstance().UpdateDetail(sysCodeDetail, "SYS_CODE_DETAIL")))
                        {
                            success = true;
                        }
                    }
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F1800_013") + ex.Message;
                    return;
                }
                finally
                {
                    base.Cache.Remove(base.UserInfo.CurrentWH + "SysCode");
                }
                if (success)
                {
                    this.hdnFrom.Value = "detail";
                    base.Response.Redirect(string.Format("SysCode.aspx?from={0}&detail={1}&ID={2}", base.UrlEncode(this.hdnFrom.Value), "2", base.UrlEncode(this.hdnSysCodeType.Value)), false);
                }
            }
            else
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1800_006");
            }
        }

        protected void btnSaveContinue_Click(object sender, EventArgs e)
        {
            bool success = false;
            if (!string.IsNullOrEmpty(this.txtSysCode.Text))
            {
                try
                {
                    SysCodeDetailInfo sysCodeDetail = new SysCodeDetailInfo {
                        SysCodeType = this.hdnSysCodeType.Value,
                        Remark = this.txtRemark.Text,
                        Description = this.txtDescription.Text
                    };
                    if (this.chkIsActive.Checked)
                    {
                        sysCodeDetail.IsActive = "Y";
                    }
                    else
                    {
                        sysCodeDetail.IsActive = "N";
                    }
                    if (this.chkSystemcreated.Checked)
                    {
                        sysCodeDetail.SystemCreated = "Y";
                    }
                    else
                    {
                        sysCodeDetail.SystemCreated = "N";
                    }
                    sysCodeDetail.SysCode = this.txtSysCode.Text;
                    sysCodeDetail.CreatedBy = base.UserInfo.UserID;
                    if (!string.IsNullOrEmpty(BaseInfoDetailServiceClientProxy.GetInstance().InsertDetail(sysCodeDetail, "SYS_CODE_DETAIL")))
                    {
                        success = true;
                    }
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F1800_013") + ex.Message;
                }
                finally
                {
                    base.Cache.Remove(base.UserInfo.CurrentWH + "SysCode");
                }
                if (success)
                {
                    this.lblMsg.Text = this.txtSysCode.Text + base.GetGlobalMsgResource("F1800_007");
                    this.txtRemark.Text = "";
                    this.txtDescription.Text = "";
                    this.txtSysCode.Text = "";
                }
            }
            else
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1800_006");
            }
        }

        private void Enabled(string Type)
        {
            string CS40000 = Type;
            if (CS40000 != null)
            {
                if (!(CS40000 == "New"))
                {
                    if (CS40000 == "Edit")
                    {
                        this.txtSysCode.Enabled = false;
                        this.txtDescription.Enabled = true;
                        this.txtRemark.Enabled = true;
                        this.chkIsActive.Enabled = true;
                        this.chkSystemcreated.Enabled = false;
                    }
                    else if (CS40000 == "IsSystemCreated")
                    {
                        this.txtSysCode.Enabled = false;
                        this.txtDescription.Enabled = false;
                        this.txtRemark.Enabled = false;
                        this.chkIsActive.Enabled = false;
                        this.chkSystemcreated.Enabled = false;
                    }
                }
                else
                {
                    this.txtSysCode.Enabled = true;
                    this.txtDescription.Enabled = true;
                    this.txtRemark.Enabled = true;
                    this.chkIsActive.Enabled = true;
                    this.chkSystemcreated.Enabled = false;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.hdnSysCodeType.Value = base.Request.QueryString["ID"];
                if (!string.IsNullOrEmpty(base.Request.QueryString["SysCode"]))
                {
                    this.btnSaveContinue.Enabled = false;
                    this.hdnType.Value = "Edit";
                    this.txtSysCode.Text = base.Request.QueryString["SysCode"];
                    SysCodeDetailInfo sysCodeDetail = BaseInfoDetailServiceClientProxy.GetInstance().GetDetailInfoByID(this.hdnSysCodeType.Value, this.txtSysCode.Text, "", "SYS_CODE_DETAIL") as SysCodeDetailInfo;
                    this.txtRemark.Text = sysCodeDetail.Remark;
                    this.txtDescription.Text = sysCodeDetail.Description;
                    this.chkIsActive.Checked = sysCodeDetail.IsActive == "Y";
                    this.chkSystemcreated.Checked = sysCodeDetail.SystemCreated == "Y";
                    this.hdnCreatedBy.Value = sysCodeDetail.CreatedBy;
                    if (this.chkSystemcreated.Checked)
                    {
                        this.Enabled("IsSystemCreated");
                    }
                    else
                    {
                        this.Enabled("Edit");
                    }
                }
                else
                {
                    this.hdnType.Value = "Add";
                    this.Enabled("New");
                    this.chkSystemcreated.Checked = false;
                }
                if (!string.IsNullOrEmpty(base.Request.QueryString["from"].ToString()))
                {
                    string from = base.Request.QueryString["from"].ToString();
                    this.hdnFrom.Value = from;
                }
            }
        }
    }
}

